<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of promotion
 *
 * @author Administrator
 */
class promotion extends CI_Controller {

  //put your code here
  public function __construct() {
    parent::__construct();
    set_userdata('end_user_current_url', current_url());
    if (!userdata('country_id') || !userdata('state_id') || !userdata('city_id')) {
      set_userdata('redirect_view', current_url());
      if (!userdata('end_user_user_id')) {
        redirect('welcome');
      } else {
        redirect('welcome/location');
      }
      return;
    }
  }

  public function index() {
    set_userdata('end_user_current_url', current_url());
    redirect('index');
  }

  public function show($id = '', $nice_name = '', $max = '', $offset = '') {
    set_userdata('end_user_current_url', current_url());
    $promotion = $this->promotion_model->get(array('id' => $id, 'status' => PROMOTION_PUBLISH));
    if ($promotion) {
      $offset = $offset + 0;
      $max = $this->setting_model->item('max_record');
      $promotion['medias'] = $this->promotion_media_model->get_all(array('promotion_id' => $id));
      $promotion['comments'] = $this->discussion_model->get_all(array('promotion_id' => $id), null, null, null, null, null, null, null, null, null, null, null, array('date_post' => 'desc'));
      $promotion['provider'] = $this->provider_user_model->get($promotion['provider_user_id']);
      $promotion['num_got'] = $this->promotion_download_model->count(array('promotion_id' => $id));
      $temp = null;
      $paging = '';
      $promotion['relates'] = array();
      $relates = $this->promotion_relate_model->get_all(array('promotion1_id' => $id, 'promotion2_id != ' => 0));
      if (!empty($relates)) {
        $temp = array('id' => array());
        foreach ($relates as $relate) {
          $temp['id'][] = $relate['promotion2_id'];
        }
        $promotion['relates'] = $this->promotion_model->get_all(array('status' => PROMOTION_PUBLISH,
            'start_date <= ' => time(), 'end_date >= ' => time(), 'remain > ' => 0), null, $temp, null, null, null, null, null, null, null, null, null, array('date_publish' => 'desc'), $max, $offset);
        foreach ($promotion['relates'] as $i => $p) {
          $promotion['relates'][$i]['medias'] = $this->promotion_media_model->get_all(array('promotion_id' => $p['id']));
          $promotion['relates'][$i]['num_got'] = $this->promotion_download_model->count(array('promotion_id' => $p['id']));
        }
        $total = $this->promotion_model->count(array('status' => PROMOTION_PUBLISH,
            'start_date <= ' => time(), 'end_date >= ' => time(), 'remain > ' => 0), null, $temp);
        $this->load->helper('paging');
        $paging = paging('index/category', $total, $max, $offset, array($id, $nice_name));
      }
      $this->load->view('user/detail_promotion', array('promotion' => $promotion,
          'captcha' => generate_captcha(200), 'paging' => $paging, 'title_html' => $promotion['title']));
    } else {
      set_flash_error($this->lang->line('error.end_user.promotion.not.found'));
      redirect('');
    }
  }

  public function download($id = '', $nice_name = '') {
    set_userdata('end_user_current_url', current_url());
    if (userdata('end_user_role') == 'END-USER') {
      $promotion = $this->promotion_model->get(array('id' => $id, 'status' => PROMOTION_PUBLISH, 'remain > ' => 0));
      if ($promotion) {
        if ($promotion['start_date'] > time()) {
          set_flash_error($this->lang->line('error.end_user.promotion.not.start'));
        } else if ($promotion['end_date'] < time()) {
          set_flash_error($this->lang->line('error.end_user.promotion.expire'));
        } else {
          $num_get = empty($_REQUEST['num_get']) ? 1 : ($_REQUEST['num_get']);
          $download = $this->db->get_where(DB_TABLE_PROMOTION_DOWNLOAD, array(
                      'promotion_id' => $id, 'end_user_id' => userdata('end_user_id'),
                      'status != ' => PROMOTION_DOWNLOAD_CANCEL,
                      'status != ' => PROMOTION_DOWNLOAD_REJECT), 1, 0)->result();
          if (!empty($download)) {
            set_flash_error($this->lang->line('error.promotion.download.already'));
          } else {
            $data = array(
                'promotion_id' => $id, 'end_user_id' => userdata('end_user_id'),
                'code' => strtoupper(substr(md5(time() . uniqid() . time()), 0, 11) . userdata('end_user_id')),
                'num_get' => $num_get, 'status' => PROMOTION_DOWNLOAD_NEW, 'date_create' => time());
            $this->load->library('promotion_lib');
            $send = $this->promotion_lib->download($data);
            if ($send) {
              $this->db->insert(DB_TABLE_PROMOTION_DOWNLOAD, $data);
              $id_download = $this->db->insert_id();
              if (!empty($id_download)) {
                $this->db->where(array('id' => $id));
                $this->db->update(DB_TABLE_PROMOTION, array('remain' => $promotion['remain'] - $num_get));
                set_flash_message($this->lang->line('message.promotion.download.success'));
              } else {
                set_flash_message($this->lang->line('error.promotion.download.fatal.error'));
              }
            } else {
              set_flash_message($this->lang->line('error.promotion.download.cannot.send.email'));
            }
          }
        }
      } else {
        set_flash_error($this->lang->line('error.end_user.promotion.not.found'));
        redirect('');
        return;
      }
    } else {
      set_flash_error($this->lang->line('message.require.login.as.end_user'));
    }
    redirect('promotion/show/' . $id . '/' . $nice_name);
  }

  public function comment($id = '', $nice_name = '') {
    if (userdata('end_user_role') == 'END-USER') {
      $promotion = $this->promotion_model->get(array('id' => $id, 'status' => PROMOTION_PUBLISH));
      if ($promotion) {
        $comment = $_REQUEST;
        $comment['promotion_id'] = $id;
        $comment['date_post'] = time();
        $comment['end_user_id'] = userdata('end_user_id');
        $this->discussion_model->save($comment);
        redirect('promotion/show/' . $id . '/' . $nice_name);
      } else {
        set_flash_error($this->lang->line('error.end_user.promotion.not.found'));
        redirect('');
      }
    } else {
      set_flash_error($this->lang->line('message.require.login.as.end_user'));
      redirect('promotion/show/' . $id . '/' . $nice_name);
    }
  }

}